/**
 * Copyright (C), 2015-2023, 成都启迪信息技术有限公司
 * FileName: UserStrokeOrderPreferentialLogHolderService
 * Author:   sjk
 * Date:     2023-03-27 16:40
 * Description:
 * <author>          <time>          <version>          <desc>
 * 作者姓名         修改时间           版本号            描述
 */
package com.qd.panda.service.stroke.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdqidi.util.JacksonUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.qd.common.panda.domain.entity.stroke.preferential.AppUserStrokeOrderPreferentialLogDisplayDTO;
import com.qd.common.panda.domain.entity.stroke.preferential.AppUserStrokeOrderPreferentialLogQueryDTO;
import com.qd.panda.service.stroke.BigDecimalUtil;
import com.qd.panda.service.stroke.DisplayUtil;
import com.qd.system.service.dict.MbUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * @author sjk
 * @create 2023-03-27 16:40
 * @since 1.0.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserStrokeOrderPreferentialLogHolderService {
    private final UserStrokeOrderPreferentialLogService userStrokeOrderPreferentialLogService;
    private final UserStrokeOrderGasService userStrokeOrderGasService;

    /**
     * 分页数据
     *
     * @param page 分页对象
     * @param dto  查询参数
     * @return 分页信息
     */
    @Transactional(readOnly = true)
    public IPage<AppUserStrokeOrderPreferentialLogDisplayDTO> appPageUserStrokeOrderPreferentialLog(Page<AppUserStrokeOrderPreferentialLogDisplayDTO> page, AppUserStrokeOrderPreferentialLogQueryDTO dto) {
        final QueryWrapper<AppUserStrokeOrderPreferentialLogQueryDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t2.pay_status", DisplayUtil.PAY_STATUS_1);
        if (null != dto.getTripType()) {
            queryWrapper.eq("t2.trip_type", dto.getTripType());
        }
        if (null != dto.getPreferentialCategory()) {
            queryWrapper.eq("t1.preferential_category", dto.getPreferentialCategory());
        }
        if (StringUtils.hasLength(dto.getUserId())) {
            queryWrapper.eq("t2.user_id", dto.getUserId());
        }
        if (StringUtils.hasLength(dto.getBeginTime())) {
            queryWrapper.apply("date_format(t1.add_time,'%Y-%m-%d') >= {0}", dto.getBeginTime());
        }
        if (StringUtils.hasLength(dto.getEndTime())) {
            queryWrapper.apply("date_format(t1.add_time,'%Y-%m-%d') <= {0}", dto.getEndTime());
        }
        queryWrapper.orderByDesc("t1.add_time");
        final IPage<AppUserStrokeOrderPreferentialLogDisplayDTO> iPage = userStrokeOrderPreferentialLogService.getBaseMapper().appPageUserStrokeOrderPreferentialLog(page, queryWrapper);
        Optional.ofNullable(iPage.getRecords()).ifPresent(l -> l.forEach(this::buildDisplay));
        queryWrapper.clear();
        return iPage;
    }

    private void buildDisplay(AppUserStrokeOrderPreferentialLogDisplayDTO dto) {
        dto.setPriceDisplay(Optional.ofNullable(dto.getPrice()).map(amount -> BigDecimalUtil.buildDefaultScale(amount).stripTrailingZeros().toPlainString()).orElse(""));
        dto.setDiscountAmountDisplay(Optional.ofNullable(dto.getDiscountAmount()).map(amount -> BigDecimalUtil.buildDefaultScale(amount).stripTrailingZeros().toPlainString()).orElse(""));
        dto.setServiceChargeDisplay(Optional.ofNullable(dto.getServiceCharge()).map(amount -> BigDecimalUtil.buildDefaultScale(amount).stripTrailingZeros().toPlainString()).orElse(""));
        dto.setTripTypeDisplay(DisplayUtil.getTripTypeDisplay(dto.getTripType()));
        dto.setPreferentialCategoryDisplay(MbUtil.idToDisplay(dto.getPreferentialCategory()));
        if (null != dto.getTripType()) {
            ObjectNode objectNode = JacksonUtil.getObjectMapper().createObjectNode();
            if (dto.getTripType().equals(DisplayUtil.TRIP_TYPE_2)) {
                //加油
                Optional.ofNullable(userStrokeOrderGasService.getById(dto.getOrderId())).ifPresent(gas -> {
                    objectNode.put("octaneNumber", gas.getOctaneNumber());
                    objectNode.put("nozzleNumber", gas.getNozzleNumber());
                });
            }
            if (!objectNode.isEmpty()) {
                dto.setExtendInfo(objectNode);
            }
        }
    }
}